
set -x
function run_build(){

number=$0

mkdir -p  /zion0/var/log/ccache
rm -rf /zion0/ccache_dir/l2_nvfs_distcc_pump_ccache_ninja_build_llvm || echo 0
mkdir -p  /zion0/ccache_dir/l2_nvfs_distcc_pump_ccache_ninja_build_llvm

rm -rf /zion0/git_proj/l2_nvfs_distcc_pump_ccache_ninja_build_llvm || echo 0
mkdir -p /zion0/git_proj/l2_nvfs_distcc_pump_ccache_ninja_build_llvm

mkdir -p /zion0/etc/ssh
cat > /zion0/etc/ssh/ssh_config <<"EOF"
Host *
StrictHostKeyChecking no
EOF

cat > /zion0/git_proj/l2_nvfs_distcc_pump_ccache_ninja_build_llvm/config_ccache.sh << "EOF"
ccache -o 'base_dir=/zion0/git_proj'
ccache -o 'cache_dir=/zion0/ccache_dir/l2_nvfs_distcc_pump_ccache_ninja_build_llvm'
ccache -o 'remote_storage='
ccache -o 'remote_only=false'
ccache -o 'recache=false'
ccache -o 'sloppiness=file_macro,include_file_mtime,time_macros,random_seed,include_file_ctime'
ccache -o 'debug=false'
ccache -o 'debug_dir=/zion0/var/log/ccache/l2_nvfs_distcc_pump_ccache_ninja_build_llvm/'

ccache -p
EOF

docker run --rm \
    -v /root/.ssh:/root/.ssh \
    -v /root/.config:/root/.config \
    -v /zion0/git_proj:/zion0/git_proj \
    -v /zion0/var/log/ccache:/zion0/var/log/ccache \
    -v /zion0/ccache_dir:/zion0/ccache_dir \
    openeuler/openeuler:22.03-lts-sp2-llvm-ccache-distcc \
    bash /zion0/git_proj/l2_nvfs_distcc_pump_ccache_ninja_build_llvm/config_ccache.sh

cat > /zion0/git_proj/l2_nvfs_distcc_pump_ccache_ninja_build_llvm.sh << "EOF"
set -eux

build_core=204

# DISTCC_POTENTIAL_HOSTS
export DISTCC_HOSTS="node02/23,lzo,cpp node03/23,lzo,cpp mstor01/79,lzo,cpp mstor02/79,lzo,cpp"

export CCACHE_PREFIX=/usr/local/bin/distcc


build_base='/zion0/git_proj/l2_nvfs_distcc_pump_ccache_ninja_build_llvm'

build_llvm=${build_base}/build-llvm
build_clang=${build_base}/build-clang
installprefix=${build_base}/install
llvm=/zion0/git_proj/llvm-project-llvmorg-18.1.0/

rm -rf $build_llvm || echo 0
mkdir -p $build_llvm
mkdir -p $installprefix

ccache -z

# start distcc-pump
eval `/usr/local/bin/pump --startup`

cd  $llvm/llvm 
time cmake \
    -G Ninja -S $llvm/llvm -B $build_llvm \
    -D CMAKE_C_COMPILER_LAUNCHER=ccache -D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
    -DLLVM_INSTALL_UTILS=ON \
    -DCMAKE_INSTALL_PREFIX=$installprefix \
    -DCMAKE_BUILD_TYPE=Release  2>&1 | tee ${build_base}/build.log

time ninja -v -C $build_llvm  -j${build_core} 2>&1 | tee ${build_base}/ninja_build.log

ccache -s

# run with cache
rm -rf $build_llvm || echo 0
mkdir -p $build_llvm
mkdir -p $installprefix

ccache -z

time cmake \
    -G Ninja -S $llvm/llvm -B $build_llvm \
    -D CMAKE_C_COMPILER_LAUNCHER=ccache -D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
    -DLLVM_INSTALL_UTILS=ON \
    -DCMAKE_INSTALL_PREFIX=$installprefix \
    -DCMAKE_BUILD_TYPE=Release  2>&1 | tee ${build_base}/build.log

time ninja -v -C $build_llvm  -j${build_core} 2>&1 | tee ${build_base}/ninja_build.log

ccache -s

# stop distcc-pump
/usr/local/bin/pump --shutdown
EOF


mkdir -p /zion0/l2build_log
chmod 777  /zion0/l2build_log

cat > /zion0/git_proj/docker_build_l2_nvfs_distcc_pump_ccache_ninja_build_llvm.sh <<"EOF"

docker rm -f build_llvm || echo 0
docker run --name build_llvm \
    --network host \
    -v /zion0/etc/ssh/ssh_config:/etc/ssh/ssh_config:ro \
    -v /root/.ssh:/root/.ssh \
    -v /root/.config:/root/.config \
    -v /zion0/git_proj:/zion0/git_proj \
    -v /zion0/var/log/ccache:/zion0/var/log/ccache \
    -v /zion0/ccache_dir:/zion0/ccache_dir \
    -v /zion0/var/log/ccache:/zion0/var/log/ccache \
    -v  /zion0/l2build_log:/var/log \
    openeuler/openeuler:22.03-lts-sp2-llvm-ccache-distcc \
    bash /zion0/git_proj/l2_nvfs_distcc_pump_ccache_ninja_build_llvm.sh
EOF


log_file="/zion0/git_proj/l2_nvfs_distcc_pump_ccache_ninja_build_llvm_${number}.log"

bash /zion0/git_proj/docker_build_l2_nvfs_distcc_pump_ccache_ninja_build_llvm.sh 2>&1 | tee ${log_file}

}

for i in `seq 1 10`
do
echo "run build $i"
run_build $i
done


# nohup bash  /zion0/loop_run_build.sh 2>&1 > /zion0/loop.log &

# tail -f /zion0/loop.log